﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Routing;
using System.Web.Compilation;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;
using VelocityShop;
using VelocityShop.Entities;

namespace VelocityShop.Web.UI
{
    public partial class CatalogViewer : System.Web.UI.UserControl
    {
        public delegate void CatalogEventHandler(object sender, CatalogEventArgs e);
        public event CatalogEventHandler OnPageIndexChanging;

        public delegate void CartItemEventHandler(object sender, CartItemEventArgs e);
        public event CartItemEventHandler OnCartItemAdding;

        private DateTime _lastUpdate;
        private int _totalItemsCount;
        private string _basePageHREF;
        private string _currentCategory;
        private string _currentTag;
        private int _currentPageNumber;

        private IList<ItemTag> _availableTags;

        public string CurrentCategory
        {
            get { return _currentCategory; }
            set { _currentCategory = value; }
        }

         public int CurrentPageNumber
        {
            get { return _currentPageNumber; }
            set { _currentPageNumber = value; }
        }

        public string CurrentTag
        {
            get { return _currentTag; }
            set { _currentTag = value; }
        }

        public IList<ItemTag> AvailableTags
        {
            get { return _availableTags; }
            set { _availableTags = value; }
        }
        
        public IEnumerable<CatalogItem> DataSource
        {
            get { return null; }
            set
            {
                repCatalog.DataSource = value;
                repCatalog.DataBind();
                BuildTagArea();
                BuildPager();
            }
        }

        public DateTime LastUpdate
        {
            get { return _lastUpdate; }
            set
            {
                _lastUpdate = value;
            }
        }

        public string BasePageHREF
        {
            get { return _basePageHREF; }
            set { _basePageHREF = value; }
        }

        public string GetPageEntryUrl(object pageNumber)
        {
            
            string expression;

            if (string.IsNullOrEmpty(CurrentTag))
                expression = String.Format("RouteName={0}, category={1}, page={2}", "Catalog", this.CurrentCategory, pageNumber);
            else
                expression = String.Format("RouteName={0}, category={1}, page={2}, tag={3}", "Catalog", this.CurrentCategory, pageNumber, CurrentTag);

            return RouteUrlExpressionBuilder.GetRouteUrl(this, expression);
        }

        public string GetTagEntryUrl(string tag)
        {
            string expression;
            expression = String.Format("RouteName={0}, category={1}, tag={2}", "Catalog", this.CurrentCategory, tag);

            return RouteUrlExpressionBuilder.GetRouteUrl(this, expression);
        }

        public class CartItemEventArgs : EventArgs
        {
            private string _productId;

            public string ProductId
            {
                get { return _productId; }
            }

            public CartItemEventArgs(string productId)
            {
                _productId = productId;
            }

        }

        public class CatalogEventArgs : EventArgs
        {
            private int _newPageNumber;
            private string _tag;

            public CatalogEventArgs(int newPageNumber, string categoryId, string tag)
            {
                _newPageNumber = newPageNumber;
                _tag = tag;
            }

            public int NewPageNumber
            {
                get { return _newPageNumber; }
            }

            public string Tag
            {
                get { return _tag; }
                set { _tag = value; }
            }
        }

        protected void AddToCart(object sender, EventArgs e)
        {
            Button btn = sender as Button;
            OnCartItemAdding(this, new CartItemEventArgs(btn.CommandArgument.ToString()));
        }

        public int PageSize
        {
            get {

                if (ViewState["CatalogPageSize"] == null)
                    ViewState["CatalogPageSize"] = 0;

                return (int)ViewState["CatalogPageSize"] ; 
            }
            set {  ViewState["CatalogPageSize"] = value; }
        }

        public int TotalItemsCount
        {
            get { return _totalItemsCount; }
            set { _totalItemsCount = value; }
        }

        private void BuildTagArea()
        {
            repTags.DataSource = AvailableTags;
            repTags.DataBind();
        }

        private void BuildPager()
        {
            int pageCount = this.TotalItemsCount / this.PageSize;
            int maxPages = 0;

            if (this.TotalItemsCount % this.PageSize > 0)
                pageCount++;

            List<PageEntry> pages = new List<PageEntry>();

            if (maxPages == 0) maxPages = pageCount;

            for (int i = 1; i < maxPages+1; i++)
            {
                pages.Add(new PageEntry(i, i.ToString()));
            }

            PagerTop.DataSource = pages;
            PagerTop.DataBind();
            PagerBottom.DataSource = pages;
            PagerBottom.DataBind();
        }

        public string GetPageEntryClass(object pageNumber)
        {
            if (this.CurrentPageNumber == (int)pageNumber)
                return "currentpage";
            else
                return "";
            
        }

        public string GetPromoClass(object isPromo)
        {
            if ((bool)isPromo)
                return "catalogItemPromo";
            else
                return "";

        }

        public class PageEntry
        {
            int _pageNumber;
            string _pageLabel;

            public int PageNumber
            {
                get { return _pageNumber; }
                set { _pageNumber = value; }
            }

            public string PageLabel
            {
                get { return _pageLabel; }
                set { _pageLabel = value; }
            }

            public PageEntry(int pageNumber, string pageLabel)
            {
                _pageNumber = pageNumber;
                _pageLabel = pageLabel;
            }
        }



    }
}